package com.example.appointment.repository;

import com.example.appointment.entity.Comment;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

public interface CommentRepository extends JpaRepository<Comment, Long> {
    Page<Comment> findByPostIdOrderByCreatedAtDesc(Long postId, Pageable pageable);
    
    @Query("SELECT c FROM Comment c WHERE c.post.id = :postId")
    List<Comment> findCommentsByPostIdDirect(@Param("postId") Long postId);
    
    @Modifying
    @Query("UPDATE Comment c SET c.likes = c.likes + 1 WHERE c.id = :commentId")
    void incrementLikes(Long commentId);
    
    @Modifying
    @Transactional
    @Query("DELETE FROM Comment c WHERE c.post.id = :postId")
    void deleteByPostId(@Param("postId") Long postId);
    
    List<Comment> findTopByOrderByCreatedAtDesc(Pageable pageable);
    
    @Query("SELECT COUNT(c) FROM Comment c")
    Long countTotalComments();
    
    @Query("SELECT COUNT(c) FROM Comment c WHERE c.createdAt >= :today")
    Long countTodayComments(@Param("today") LocalDateTime today);
} 